package cn.lg.soar.message.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.PojoUtil;
import cn.lg.soar.common.util.current.UserContext;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.core.annotations.constraints.Id;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.message.api.constant.ModuleConst;
import cn.lg.soar.message.api.enums.NoticeRecipientTypeEnum;
import cn.lg.soar.message.entity.Notice;
import cn.lg.soar.message.model.NoticeAddVo;
import cn.lg.soar.message.model.NoticeDetailVo;
import cn.lg.soar.message.model.NoticeVo;
import cn.lg.soar.message.service.INoticeService;
import cn.lg.soar.mvc.annotations.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Validated
@Api(tags = "通知管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/notice")
public class NoticeController extends BaseController<Notice, INoticeService> {

    @Override
    protected Serializable getId(Notice entity) {
        return entity.getId();
    }

    private final static String SUBJECT = "通知";

    @Log(value = "新增" + SUBJECT, data = "#p0", dataId = "#$r.data")
    @IdempotentByParam("#p0")
    @CreateMapping
    public HttpResult<Void> create(@RequestBody NoticeAddVo data) {
        NoticeRecipientTypeEnum recipientType = data.getRecipientType();
        if (NoticeRecipientTypeEnum.named.equals(recipientType)) {
            service.toUser(PojoUtil.copy(data, Notice.class), data.getUserIds());
        } else {
            service.broadcast(PojoUtil.copy(data, Notice.class));
        }
        return HttpResult.success();
    }

    @Log(value = "删除" + SUBJECT, data = "#p0", type = Log.Type.TEXT)
    @RemoveMapping
    public HttpResult<Void> remove(@RequestBody List<Long> ids) {
        return super.removeResult(ids);
    }

    @DetailMapping
    public HttpResult<NoticeDetailVo> detail(@Id Long id) {
        return HttpResult.data(this.service.detail(id));
    }

    @QueryMapping
    public HttpResult<PageResult<Notice>> query(PageQuery pageQuery, QueryParams params){
        return queryResult(pageQuery, params);
    }
    
    @Log(value = "导出" + SUBJECT, data = "#p0")
    @ExportMapping
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel("通知", params, resp);
    }

    @Log(value = "导入" + SUBJECT)
    @ImportMapping
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @ListMapping
    public HttpResult<PageResult<NoticeVo>> list(PageQuery pageQuery, QueryParams params){
        IPage<NoticeVo> page = service.page(pageQuery, params);
        PageResult<NoticeVo> result = new PageResult<>(page.getTotal(), page.getRecords());
        result.setPageNo(pageQuery.getPageNo());
        result.setPageSize(pageQuery.getPageSize());
        result.setQueryType(pageQuery.getQueryType());
        return HttpResult.data(result);
    }

    @ApiOperation("未读消息")
    @GetMapping("unread-list")
    public HttpResult<PageResult<Notice>> list(PageQuery pageQuery){
        IPage<Notice> page = service.unreadNoticeList(PageUtils.toPage(pageQuery), UserContext.getUserId());
        return HttpResult.data(PageUtils.toPageResult(page));
    }

    @ApiOperation("更新已读")
    @PostMapping("update-read")
    public HttpResult<Void> updateRead(@RequestBody List<Long> ids) {
        service.updateRead(ids, UserContext.getUserId());
        return HttpResult.success();
    }

    @ApiOperation("读取消息")
    @GetMapping("read")
    public HttpResult<Notice> read(@RequestParam long id) {
        return HttpResult.data(service.read(id));
    }

}
